package com.example.forum.dao;

import com.example.forum.domain.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface MessageDao {
    //查询包含一个用户的所有信息,按照时间排序
    @Select("select * from message where from_id=#{fromToId} or to_id=#{fromToId} order by create_time")
    public List<Message> findByFromToId(int fromToId);

    //查询两个用户之间的最后一条信息
    @Select("select * from message where from_id=#{fromToId} or to_id=#{fromToId} order by create_time")
    public List<Message> findLastByFromToId(int fromToId);

    //查询两个用户之间会话次数
    @Select("select count(*) from message where conversation_id=#{conversationId}")
    public int findCount(String conversationId);

    //查询两个用户的消息的未读条数
    @Select("select count(*) from message where from_id=#{fromId} and to_id=#{toId} and status=0")
    public int findUnreadCount(@Param("fromId")int fromId,@Param("toId")int toId);

    //查询两个用户的所有对话
    @Select("select * from message where conversation_id=#{conversationId}")
    public List<Message> findAllSession(String conversationId);

    //查询一个用户的所有未读消息条数
    @Select("select count(*) from message where to_id=#{Id} and status=0")
    public int totalUnreadCount(int toId);
} 
